package com.gexiaobao.pigeon.ui.fragment.mine.tools.line

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.FlyWeatherInfoResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentToolMapWeatherBinding
import com.gexiaobao.pigeon.ui.adapter.MapWeatherAdapter
import com.gexiaobao.pigeon.viewmodel.MatchLineQueryViewModel

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 9:29
 * @Describe    :
 */
class FragmentToolsMapWeather : BaseFragment<MatchLineQueryViewModel, FragmentToolMapWeatherBinding>() {

    private val mAdapter: MapWeatherAdapter by lazy { MapWeatherAdapter(arrayListOf()) }
    private var dataWeatherInfo: ArrayList<FlyWeatherInfoResponse.WeatherListInfo> = arrayListOf()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )
        setAdapterData()

        mDatabind.layoutList.swipeRefreshLayout.init {
            setAdapterData()
        }
    }

    private fun setAdapterData() {
        try {
            val flyWeatherResultList = arguments?.getParcelableArrayList<FlyWeatherInfoResponse.WeatherListInfo>("fly_weather_result_list")
            dataWeatherInfo.clear()
            if (flyWeatherResultList!!.size != 0) {
                dataWeatherInfo.addAll(flyWeatherResultList)
                mAdapter.setList(dataWeatherInfo)
            }
            if (mDatabind.layoutList.swipeRefreshLayout.isRefreshing) {
                mDatabind.layoutList.swipeRefreshLayout.isRefreshing = false
            }
        } catch (e: Exception) {
            RxToast.showToast("获取赛线信息错误101")
            e.printStackTrace()
        }
    }

    companion object {
        fun newInstance(index: ArrayList<FlyWeatherInfoResponse.WeatherListInfo>): FragmentToolsMapWeather {
            val fragment = FragmentToolsMapWeather()
            val bundle = Bundle()
            fragment.arguments = bundle
            bundle.putParcelableArrayList("fly_weather_result_list", index)
            return fragment
        }
    }
}